﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using RestfulApiV1.Data.Entity;
using RestfulApiV1.Implement.Paged;
using RestfulApiV1.Interface.Data;
using RestfulApiV1.Utils.Json;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace RestfulApiV1.Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ArticleTypeController : ControllerBase
    {

        private readonly IRespository<ArticleType> _articleTypeRep;

        public ArticleTypeController(IRespository<ArticleType> articleTypeRep)
        {
            _articleTypeRep = articleTypeRep;
        }
        // GET: api/<ArticleTypeController>
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/<ArticleTypeController>/5
        [HttpGet("{id}")]
        public string Get(int pageIndex = 1, int pageSize = 10)
        {
            var list = _articleTypeRep.Table.Where(x => x.IsActived == true && x.IsDeleted == false);

            var res = new
            {
                code = 1000,
                data = GetDynamic(new Pagination<ArticleType>(list, pageIndex, pageSize)),
                msg = "获取文章分类列表成功"
            };

            return JsonHelper.SerializeObject(res);
        }

        // POST api/<ArticleTypeController>
        [HttpPost]
        public string Post([FromBody] ArticleType item)
        {
            _articleTypeRep.Add(item);

            var res = new
            {
                code = 1000,
                data = GetDynamic(item),
                msg = "增加文章分类成功"
            };
            return JsonHelper.SerializeObject(res);
        }

        // PUT api/<ArticleTypeController>/5
        [HttpPut("{id}")]
        public string Put(int id, [FromBody] ArticleType value)
        {
            var item = _articleTypeRep.GetById(id);
            dynamic res;
            if (item != null)
            {
                item.TypeName = value.TypeName;
                item.Description = value.Description;
                item.ArticleTypeId = value.ArticleTypeId;
                _articleTypeRep.Update(item);
                res = new
                {
                    code = 1000,
                    data = GetDynamic(item),
                    msg = "修改文章分类成功"
                };
            }
            else
            {
                res = new
                {
                    code = 1000,
                    data = "",
                    msg = "当前文章分类不存在"
                };
            }
            return JsonHelper.SerializeObject(res);
        }

        // DELETE api/<ArticleTypeController>/5
        [HttpDelete("{id}")]
        public string Delete(int id)
        {
            var item = _articleTypeRep.GetById(id);
            dynamic res;
            if (item != null)
            {
                item.IsDeleted = true;
                _articleTypeRep.Update(item);
                res = new
                {
                    code = 1000,
                    data = "",
                    msg = "删除文章分类成功"
                };
            }
            else
            {
                res = new
                {
                    code = 1000,
                    data = "",
                    msg = "当前文章分类不存在"
                };
            }
            return JsonHelper.SerializeObject(res);
        }

        [HttpPost, Route("{id}/status")]
        public string UpdateTypeStatus(int id)
        {
            var item = _articleTypeRep.GetById(id);
            dynamic res;

            if (item != null)
            {
                item.IsActived = !item.IsActived;
                _articleTypeRep.Update(item);

                res = new
                {
                    Code = 1000,
                    Data = GetDynamic(item),
                    Msg = "修改分类状态成功"
                };
            }
            else
            {
                res = new
                {
                    Code = 1002,
                    Data = "",
                    Msg = "指定分类不存在"
                };
            }


            return JsonHelper.SerializeObject(res);
        }

        private dynamic GetDynamic(ArticleType type)
        {
            var res = new
            {
                type.Id,
                type.TypeName,
                type.Description,
                type.Remarks,
                IsActived = type.IsActived ? 1 : 0,
                type.ArticleTypeId
            };
            return res;
        }

        private dynamic GetDynamic(Pagination<ArticleType> types)
        {
            var list = new List<dynamic>();
            foreach (var type in types.List)
            {
                var temp = GetDynamic(type);
                list.Add(temp);
            }
            var data = new
            {
                types.PageIndex,
                types.PageSize,
                types.TotalItem,
                types.TotalPage,
                List = list
            };
            return data;
        }
    }
}
